package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.io.Console;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HttpSession session;

    //员工登录
    @Override
    public ResultInfo login(String username, String password) {
        Employee employee = employeeMapper.findByUsername(username);

        //判断数据库是否有此用户
        if (employee == null) {
           // return ResultInfo.error("你输入的用户名不正确");
            throw new CustomException("你输入的用户名不正确");
        }

        //判断密码是否正确 因为是加密的 所以输入的密码也需要加密
        String passwordInput = SecureUtil.md5(password);
        if (!StrUtil.equals(passwordInput, employee.getPassword())) {
           // return ResultInfo.error("你输入密码不正确");
            throw new CustomException("你输入密码不正确");
        }

        //判断此账号是否启用
        if (employee.getStatus() != 1) {
            //return ResultInfo.error("此账号被禁用请联系管理员");
            throw new CustomException("此账号被禁用请联系管理员");
        }
        session.setAttribute(Constant.SESSION_EMPLOYEE,employee);
        //判断完了 都没问题就要成功登录了
        return ResultInfo.success(employee);
    }

    //员工查询 按名字
    @Override
    public ResultInfo finedBynaame(String name) {

        List<Employee> employees = employeeMapper.finedByname(name);

        return ResultInfo.success(employees);
    }

    //新增员工
    @Override
    public void save(Employee employee) {
        long Id = IdUtil.createSnowflake(1, 1).nextId();
        employee.setId(Id);
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());

        String passwordDeauflt = SecureUtil.md5(Constant.INIT_PASSWORD);
        //存入默认密码
        employee.setPassword(passwordDeauflt);
        //账号默认设置启用
        employee.setStatus(Employee.STATUS_ENABLE);

        //从session中获取登录对象的信息
        Employee cureentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);

//        employee.setCreateUser(cureentEmp.getCreateUser());
//        employee.setUpdateUser(cureentEmp.getCreateUser());

       // employeeMapper.save(employee);
        employeeMapper.insert(employee);



    }
     //查询回显
    @Override
    public ResultInfo findByid(Long id) {
        Employee employee = employeeMapper.findByid(id);
       return ResultInfo.success(employee);
    }
     //修改员工
    @Override
    public ResultInfo updateByID(Employee employee) {
      
//        //写修改时间
//           employee.setUpdateTime(new Date());
//            //获得创建人信息
//        Employee attribute = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
//          employee.setUpdateUser(attribute.getId());

 //       employeeMapper.updateByID(employee);
        employeeMapper.updateById(employee);
        return ResultInfo.success();
    }


}
